<template>
  <div class="global-box">
    <div class="title">
      <h1>{{ title }}</h1>
      <a-button type="primary" @click="addCourse('add')">添加考试</a-button>
    </div>
    <a-card :body-style="{ padding: '24px' }" :bordered="false">
      <div class="form-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="课程目录">
                <a-cascader
                  :field-names="{ label: 'value', value: 'id', children: 'children' }"
                  :options="directoryOptions"
                  :load-data="loadData"
                  placeholder="请选择课程目录"
                  change-on-select
                  v-model="queryParam.directory"
                />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="考期">
                <a-input v-model="queryParam.examinationType" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="search">筛选</a-button>
                <a-button style="margin-left: 8px" @click="reset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-wrapper">
        <a-table :columns="columns" :data-source="data" :pagination="pagination">
          <span slot="bmNot" slot-scope="text, record">
            <template>
              <span v-if="record.bmPublishedStartTime">{{
                record.bmPublishedStartTime + ' 至 ' + record.bmPublishedEndTime
              }}</span>
            </template>
          </span>
          <span slot="ksNot" slot-scope="text, record">
            <template>
              <span v-if="record.ksPublishedStartTime">{{
                record.ksPublishedStartTime + ' 至 ' + record.ksPublishedEndTime
              }}</span>
            </template>
          </span>
          <span slot="cjNot" slot-scope="text, record">
            <template>
              <span v-if="record.cjPublishedStartTime">{{
                record.cjPublishedStartTime + ' 至 ' + record.cjPublishedEndTime
              }}</span>
            </template>
          </span>
          <span slot="enable" slot-scope="text, record">
            <template>
              {{ record.enable === 0 ? '禁用' : '启用' }}
            </template>
          </span>
          <span slot="action" slot-scope="text, record">
            <template>
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
              <a @click="handleDelete(record)">删除</a>
              <a-divider type="vertical" />
              <a-popconfirm
                placement="top"
                ok-text="确认"
                cancel-text="取消"
                @confirm="handleToggle(record)"
                v-if="record.enable === 1"
              >
                <template slot="title">
                  <p>你确定要禁用该课程吗？</p>
                </template>
                <a>禁用</a>
              </a-popconfirm>
              <a v-else @click="handleToggle(record)">启用</a>
            </template>
          </span>
        </a-table>
      </div>
    </a-card>
  </div>
</template>

<script>
import { directory, list, setActive, deletes } from '@/api/examination/exam'
const columns = [
  {
    title: '考试ID',
    dataIndex: 'id',
    align: 'center'
  },
  {
    title: '考试名称',
    dataIndex: 'examinationName',
    align: 'center'
  },
  {
    title: '课程目录',
    dataIndex: 'examName',
    align: 'center'
  },
  {
    title: '考期',
    dataIndex: 'examinationYear',
    align: 'center'
  },
  {
    title: '报名日期',
    dataIndex: 'bmNot',
    scopedSlots: { customRender: 'bmNot' },
    align: 'center'
  },
  {
    title: '考试日期',
    dataIndex: 'ksNot',
    scopedSlots: { customRender: 'ksNot' },
    align: 'center'
  },
  {
    title: '成绩公布日期',
    dataIndex: 'cjNot',
    scopedSlots: { customRender: 'cjNot' },
    align: 'center'
  },
  {
    title: '启用状态',
    dataIndex: 'enable',
    scopedSlots: { customRender: 'enable' },
    align: 'center'
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center'
  }
]
export default {
  data () {
    return {
      title: '考试管理',
      queryParam: {
        directory: [],
        examinationType: ''
      },
      columns,
      data: [],
      pagination: {
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        showSizeChanger: true,
        showQuickJumper: true,
        onShowSizeChange: (current, pageSize) => {
          this.pagination.defaultCurrent = 1
          this.pagination.defaultPageSize = pageSize
        },
        // 改变每页数量时更新显示
        onChange: (current, size) => {
          this.pagination.defaultCurrent = current
          this.pagination.defaultPageSize = size
        }
      },
      directoryOptions: []
    }
  },
  computed: {
  },
  mounted () {
    this.getDirectory()
    this.getData()
  },
  methods: {
    // 获取列表数据
    getData () {
      const data = {
        currentPage: this.pagination.defaultCurrent,
        pageSize: this.pagination.defaultPageSize
      }
      if (this.queryParam.directory[0]) {
        data.directoryId = this.queryParam.directory[0]
      }
      if (this.queryParam.directory[1]) {
        data.examId = this.queryParam.directory[1]
      }
      if (this.queryParam.examinationType !== '') {
        data.examinationType = this.queryParam.examinationType
      }
      list(data).then(res => {
        if (res.code === '1') {
          this.pagination.total = res.data.totalSize
          this.data = res.data.rows
          this.data.forEach(item => {
            item.bmNotPublishedEndTime = item.bmNotPublishedEndTime && item.bmNotPublishedEndTime.split(' ')[0]
            item.bmNotPublishedStartTime = item.bmNotPublishedStartTime && item.bmNotPublishedStartTime.split(' ')[0]
            item.bmPublishedEndTime = item.bmPublishedEndTime && item.bmPublishedEndTime.split(' ')[0]
            item.bmPublishedStartTime = item.bmPublishedStartTime && item.bmPublishedStartTime.split(' ')[0]
            item.cjNotPublishedEndTime = item.cjNotPublishedEndTime && item.cjNotPublishedEndTime.split(' ')[0]
            item.cjNotPublishedStartTime = item.cjNotPublishedStartTime && item.cjNotPublishedStartTime.split(' ')[0]
            item.cjPublishedEndTime = item.cjPublishedEndTime && item.cjPublishedEndTime.split(' ')[0]
            item.cjPublishedStartTime = item.cjPublishedStartTime && item.cjPublishedStartTime.split(' ')[0]
            item.ksNotPublishedEndTime = item.ksNotPublishedEndTime && item.ksNotPublishedEndTime.split(' ')[0]
            item.ksNotPublishedStartTime = item.ksNotPublishedStartTime && item.ksNotPublishedStartTime.split(' ')[0]
            item.ksPublishedEndTime = item.ksPublishedEndTime && item.ksPublishedEndTime.split(' ')[0]
            item.ksPublishedStartTime = item.ksPublishedStartTime && item.ksPublishedStartTime.split(' ')[0]
          })
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 添加考试
    addCourse () {
      this.$router.push({
        name: 'examCommon'
      })
    },
    // 表格编辑
    handleEdit (record) {
      this.$router.push({
        name: 'examCommon',
        params: {
          re: record
        }
      })
    },
    // 表格删除
    handleDelete (record) {
      const data = {
        id: record.id
      }
      deletes(data).then(res => {
        if (res.code === '1') {
          this.$message.success(res.msg)
          this.getData()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 表格启用禁用
    handleToggle (record) {
      const data = {
        id: record.id,
        type: record.enable === 0 ? 1 : 0
      }
      setActive(data).then(res => {
        if (res.code === '1') {
          this.$message.success(res.msg)
          this.getData()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 筛选
    search () {
      this.getData()
    },
    // 重置
    reset () {
      this.queryParam = {
        directory: [],
        examinationType: ''
      }
      this.getData()
    },
    // 获取课程目录
    getDirectory () {
      const data = {
        type: 0,
        directoryLevel: 1
      }
      directory(data).then(res => {
        if (res.code === '1') {
          res.data.forEach(item => {
            item.isLeaf = false
          })
          this.directoryOptions = res.data
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    // 课程目录二级-六级
    loadData (selectedOptions) {
      if (this.queryParam.directory[1]) {
        return false
      }
      const targetOption = selectedOptions[selectedOptions.length - 1]
      const data = {
        type: 1,
        parentId: targetOption.id,
        directoryLevel: 2
      }
      targetOption.loading = true
      directory(data).then(res => {
        if (res.code === '1') {
          if (res.data.length > 0) {
            res.data.forEach(item => {
              item.isLeaf = false
            })
            targetOption.loading = false
            targetOption.children = [...res.data]
          } else {
            targetOption.loading = false
            this.$message.error('暂无更多数据')
          }
        } else {
          targetOption.loading = false
          this.$message.error(res.msg)
        }
        this.directoryOptions = [...this.directoryOptions]
      }).catch(err => {
        this.$message.error(err.msg)
        targetOption.loading = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
.form-status-tip {
  text-align: center;
  color: #999;
}
</style>
